﻿#region

using System.ComponentModel.DataAnnotations;

#endregion

namespace Vin.Extension.Framework.Attributes.Validation;

public class OptionalAttribute(string[] propertyNames) : ValidationAttribute
{
    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
    {
        if (value != null) return ValidationResult.Success;
        foreach (var propertyName in propertyNames)
        {
            var displayObj = validationContext.ObjectType.GetProperty(propertyName);
            var displayValue = displayObj?.GetValue(validationContext.ObjectInstance);
            if (displayValue == null) return new ValidationResult(ErrorMessage);
        }
        return ValidationResult.Success;
    }
}